﻿
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using QQZiFramework;
using System;
using QQZiFramework.Mvc;
using Microsoft.AspNetCore.Routing;

namespace QQZiCore.Mvc
{
    public class QQZiRequestMiddleware
    {
        private readonly RequestDelegate _next;

        public QQZiRequestMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            context.Request.EnableBuffering();
            context.Items["__START_TIME"] = DateTime.Now;
            await _next(context);            
            if (context.Items.ContainsKey(WebHostMemberShip.DBCONTEXT_NAME_IN_HTTP_CONTEXT))
            {
                //使用loginCheck过滤器里面的context
                var defaultContext = WebHostMemberShip.GetCurrentContext(context);
                if (defaultContext != null)   defaultContext.Dispose();
            }
            context.Items.Clear();
        }

    }
}